﻿
/// <reference path="vswd-ext_2.2.js" />
/********************poi.js*********************************
 *Create Date: 2011-6
 *Description : this js file use ext js to create AJAX POI function window 
*********************************************************************/

function ShowEditOrAddDriverWindow(editData, store, isShowInfo) {
    var isEdit = editData != null ? true : false;
    var isReadOnly = isShowInfo == true ? true : false;
    var driverName = new Ext.form.TextField({
        allowBlank: true,
        maxLength: 32,
        emptyText: "",
        maxText: _Message_MaxText + ' 32'
    });

    var form = new Ext.FormPanel({
        width: 375,
        autoHeight: true,
        plain: true,
        layout: "form",
        defaultType: "textfield",
        labelWidth: 100,
        plain: true,
        baseCls: "x-plain",
        defaults: { anchor: "95%", msgTarget: "side" },
        buttonAlign: "center",
        bodyStyle: "padding:0 0 0 0",
        items: [{
            name: "driver_name",
            fieldLabel: _DriverResource_Name,
            maxLength: 255,
            maxText: '255' + _Message_MaxText,
            allowBlank: false,
            readOnly: isReadOnly
        }, {
            xtype: 'datefield',
            name: "driver_age",
            allowBlank: false,
            blankText: _NewForText_CanNotEmpty,
            format: "Y-m-d",
            fieldLabel: _DriverResource_Age,
            maxLength: 255,
            maxText: _Message_MaxText,
            readOnly: isReadOnly
        }, {
            name: "driver_email",
            fieldLabel: _TAInfoResource_Email,
            maxLength: 255,
            maxText: '255 ' + _Message_MaxText,
            vtype: 'email',
            vtypeText: _NewForText_EmailWrong
        }, {
            name: "driver_phone",
            fieldLabel: _DriverResource_Phone,
            allowBlank: true,
            maxLength: 32,
            maxText: '32 ' + _Message_MaxText,
            regex: /^[0-9.]{5,32}$/,
            regexText: _NewForText_PhoneValid,
            readOnly: isReadOnly
        }, {
            name: "driver_cellphone",
            fieldLabel: _DriverResource_CellPhone,
            allowBlank: true,
            maxLength: 32,
            maxText: '32 ' + _Message_MaxText,
            regex: /^[0-9.]{5,32}$/,
            regexText: _NewForText_PhoneValid,
            readOnly: isReadOnly
        }, {
            xtype: 'textarea',
            name: "driver_remarks",
            fieldLabel: _DriverResource_Remark,
            readOnly: isReadOnly
        }
]
    });

    var mybuttons = new Array();
    
    if (isEdit) {
        mybuttons.push({
            text: _CustomerConfigResource_Update,
            minWidth: 80,
            handler: function() {
                if (form.getForm().isValid()) {
                    Ext.MessageBox.show({
                        msg: _CustomerConfigResource_Saving + "...",
                        progressText: _CustomerConfigResource_Saving + "...",
                        width: 300,
                        wait: true,
                        waitConfig: { interval: 200 },
                        icon: 'download',
                        animEl: 'saving'
                    });
                    form.getForm().submit({
                        url: "HandlerADUDriver",
                        method: "POST",
                        success: function(form, action) {
                            var flag = action.result.success;
                            if (flag == true) {
                                Ext.MessageBox.hide();
                                msgUpdateSuccess();
                                win.hide();
                                store.reload({
                                    params: {
                                        namekey: ''
                                    }
                                });
                            }
                        },
                        failure: function(form, action) {
                            msgUpdateFailure();
                        },
                        params: {
                            driver_id: editData.data.driver_id,
                            controlType: 'update'
                        }
                    });
                }
            }
        });
    }
    else {
        mybuttons.push({
            text: _NewForText_Confirm,
            handler: function() {
                if (form.getForm().isValid()) {
                    form.getForm().submit({
                        url: "HandlerADUDriver",
                        method: "POST",
                        success: function(form, action) {
                            var flag = action.result.success;
                            if (flag == true) {
                                Ext.MessageBox.hide();
                                msgUpdateSuccess();
                                win.hide();
                                store.reload({
                                    params: {
                                        namekey: ''
                                    }
                                });
                            }
                        },
                        failure: function(form, action) {
                            msgUpdateFailure();
                        },
                        params: {
                            driver_id: 0,
                            controlType: 'add'
                        }
                    });
                }
            }
        });
    }
    mybuttons.push({
        text: _NewForText_Reset,
        handler: function() {
            if (isEdit) {
                form.getForm().loadRecord(editData);
            }
            else {
                form.getForm().reset();
            }
        }
    });
    mybuttons.push({
        text: _NewForText_Cancel,
        minWidth: 80,
        handler: function() {
            win.close();
            win.destroy();
        }
    });

    if (isReadOnly)
        mybuttons = [];
    var win = new Ext.Window({
        width: 410,
        autoHeight: true,
        iconCls: "editicon",
        resizable: false,
        defaultType: "textfield",
        labelWidth: 100,
        collapsible: true,
        closable: true,
        plain: true,
        modal: 'true',
        buttonAlign: "center",
        bodyStyle: "padding:8px 0 0 10px",
        items: [form],
        listeners: {
            "show": function() {
                if (editData) {
                    form.getForm().loadRecord(editData);
                }
            }
        },
        constrain:true,
        buttons: mybuttons
    });
    win.show();
}

function DriverWindow() {
    var driverfields = ["driver_id", "driver_name", "driver_age", "driver_email", "driver_phone", "driver_cellphone", "driver_license_model1", "driver_license_model2", "driver_license_model3","driver_creator_type","driver_account","driver_remarks"];
    var driverstore = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: "HandlerSearchDriver",
            method: "POST"
        }),
        reader: new Ext.data.JsonReader({
            fields: driverfields,
            root: "data",
            totalProperty: "totalCount"
        }),
        autoLoad: true
    });

    var txtKey = new Ext.form.TextField({
        allowBlank: true,
        maxLength: 32,
        emptyText: "",
        maxText: _Message_MaxText + ' 32'
    });

    var tbar = new Ext.Toolbar({
        items: [
            txtKey, {
                tooltip: { text: _POI_Search, autoHide: true },
                icon: 'Content/images/search16.gif',
                handler: function() {
                    driverstore.reload({
                        params: {
                            namekey: txtKey.getValue()
                        }
                    });
                }
            }, {
                xtype: 'tbseparator', width: 5
            }, {
                tooltip: { text: _DriverResource_AddNewDriver, autoHide: true },
                icon: 'Content/images/add.png',
                handler: function() {
                    ShowEditOrAddDriverWindow(null, driverstore);
                }
            }, {
                tooltip: { text: _DriverResource_EditDriver, autoHide: true },
                icon: 'Content/images/edit.gif',
                handler: function() {
                    if (grid.getSelectionModel().getCount() == 1) {
                        ShowEditOrAddDriverWindow(grid.getSelectionModel().getSelections()[0], driverstore);
                    }
                    else {
                        Ext.MessageBox.buttonText.ok = _TrackerMain_OK;
                        Ext.Msg.show({
                            title: _TrackerMain_Title,
                            msg: _POI_SelectOne,
                            buttons: Ext.Msg.OK,
                            animEl: 'elId',
                            icon: Ext.MessageBox.INFO
                        });
                    }
                }
            }, {
                tooltip: { text: _DriverResource_DeleteDriver, autoHide: true },
                icon: 'Content/images/delete.png',
                handler: function() {
                    if (grid.getSelectionModel().getCount() == 1) {
                        Ext.MessageBox.YESNO = { yes: _TrackerMain_Yes, no: _TrackerMain_No };
                        Ext.MessageBox.confirm(_TrackerMain_Title, _POI_SureToDelete, function(btn) {
                            if (btn == "yes") {
                                Ext.Ajax.request({
                                    url: 'HandlerADUDriver',
                                    success: function(a, action) {
                                        var flag = Ext.util.JSON.decode(a.responseText).success;
                                        if (flag == true) {
                                            Ext.MessageBox.hide();
                                            msgUpdateSuccess();
                                            driverstore.reload({
                                                params: {
                                                    namekey: ''
                                                }
                                            });
                                        }
                                    },
                                    failure: function() {
                                        msgUpdateFailure();
                                    },
                                    params: {
                                        driver_id: grid.getSelectionModel().getSelections()[0].data.driver_id,
                                        controlType: 'delete'
                                    }
                                });
                            }
                        }, this);
                    }
                    else {
                        Ext.MessageBox.buttonText.ok = _TrackerMain_OK;
                        Ext.Msg.show({
                            title: _TrackerMain_Title,
                            msg: _POI_SelectAtLeastOne,
                            buttons: Ext.Msg.OK,
                            animEl: 'elId',
                            icon: Ext.MessageBox.INFO
                        });
                    }
                }
            }
        ]
    });

    var grid = new Ext.grid.GridPanel({
        store: driverstore,
        columns: [
                { header: _DriverResource_Name, width: 120, sortable: true, menuDisabled: true, dataIndex: 'driver_name' },
                { header: _DriverResource_Age, width: 100, sortable: true, menuDisabled: true, dataIndex: 'driver_age' },
                { header: _DriverResource_Phone, width: 150, sortable: true, menuDisabled: true, dataIndex: 'driver_phone' },
                { header: _DriverResource_CellPhone, width: 120, sortable: true, menuDisabled: true, dataIndex: 'driver_cellphone' }
//                ,{ header: _DriverResource_FirstLicense, width: 150, sortable: true, menuDisabled: true, dataIndex: 'driver_license_model1' }
            ],
        width: 530,
        height: 200
    });

    grid.addListener('rowdblclick', function(obj, index) {
        var record = grid.getStore().getAt(index);
        ShowEditOrAddDriverWindow(record, null, true);

    });

    var driverwindow = new Ext.Window({
        title: _DriverResource_Title,
        layout: 'fit',
        height: 250,
        width: 520,
        items: grid,
        constrain:true,
        
        tbar: tbar
    });

    driverwindow.show();
}